package com.atguigu.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
     private PermissionDao permissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
        //获取所有权限的列表
      List<Permission> permissionList=  permissionDao.findAll();
        //获取当前角色的权限列表 id
      List<Long> permissionIdList= this.permissionDao.findPermissionByRoleId(roleId);

      //根据当前角色权限id列表来准备zTree数据
        List<Map<String,Object>> zNodes=new ArrayList<>();
        //对所有权限进行遍历
        for (Permission permission: permissionList){
            Map<String,Object>zNode=new HashMap<>();
            zNode.put("id",permission.getId());
            zNode.put("pId",permission.getParentId());
            zNode.put("name",permission.getName());
            zNode.put("checked",permissionList.contains(permission.getId()));
            //zNode.put("open",);
            zNodes.add(zNode);



        }
        return zNodes;
    }
    @Autowired
   private RolePermissionDao rolePermissionDao;
    @Override
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {
         //删除之前的权限
         rolePermissionDao.deleteByRoleId(roleId);
        //添加新的权限
        if(permissionIds!=null&&"".equals(permissionIds)){
            for (Long permissionId:permissionIds){
                RolePermission rolePermission =new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                     rolePermissionDao.insert(rolePermission);
            }
        }


    }

    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {
        //获取用户的权限列表
         List<Permission> permissionList =null;  //不带下级
         if(adminId==1){
            permissionList= permissionDao.findAllMenuPermission();
         }else{
       permissionList= permissionDao.findMenuPermissionByAdminId(adminId);
         }
         List<Permission> result=null; //带下级
         result=  PermissionHelper.bulid(permissionList);

        return result;
    }

    @Override
    public List<String> findCodeListByAdminId(Long adminId) {
        //获取用户的权限列表
        List<String> codeList =null;  //不带下级
        if(adminId==1){
            codeList= permissionDao.findAllCodeList();
        }else{
            codeList= permissionDao.findCodeListByAdminId(adminId);
        }

        return codeList;
    }
}
